package br.com.cfccultura.admin.view.report;

import java.io.InputStream;
import java.util.Collection;
import java.util.HashMap;

import javax.servlet.http.HttpServletResponse;

import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import br.com.cfccultura.admin.model.vo.ValueObject;

public class ReportManager {
	
	public enum Type {
		COMPROVANTE_MATRICULA("comprovanteMatricula.jasper"),
		RECIBO("recibo.jasper"),
		CERTIFICADO_TAXI("certificadoTaxi.jasper"),
		TERMO_CREDENCIAMENTO_TAXI("termoCredenciamentoTaxi.jasper");
		
		private String fileName;
		
		Type(String fileName) {
			this.fileName = fileName;
		}
		
		public String getFileName() {
			return fileName;
		}
	}
	
	public void print(Type type, HttpServletResponse response, HashMap<String, Object> params, Collection<ValueObject> ds) throws Exception {
		
		response.setContentType("application/pdf");
        response.addHeader("Content-disposition", "filename=\"comprovante.pdf\"");
		
		JasperPrint print = 
			JasperFillManager.fillReport(loadFileOnDisk(type), params, new JRBeanCollectionDataSource(ds));
		JasperExportManager.exportReportToPdfStream(print, response.getOutputStream());		
	}
	
	private InputStream loadFileOnDisk(Type type) {
		InputStream in = 
			getClass().getClassLoader().getResourceAsStream("br/com/cfccultura/admin/view/report/src/" + type.getFileName());
		return in;
	}

}
